package com.utils;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Exclude {

    ///get XML exclusions from xml file
    static public Element getXmlExclusions(String fileXmlExclusions) throws ParserConfigurationException, SAXException, IOException	{

    	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    	DocumentBuilder db = dbf.newDocumentBuilder();
    	File file = new File(fileXmlExclusions);

    	Document doc = db.parse(file);
    	Element docEle = doc.getDocumentElement();

    	return docEle;
    }
    
    ///check if XML exclusion exists in plc symbol table 
    static public boolean isExclusion(Element docEle, String sSymName, String sSymIONum, String sSymType, String sSymComment) throws SAXException {

    	///check for symbol exclusions
    	NodeList nlExclusionSymbol = docEle.getElementsByTagName("symbol");

    	if (nlExclusionSymbol != null && nlExclusionSymbol.getLength() > 0) {

    		String sName;
    		String sAddressType;
    		String sComment;

    		for (int i = 0; i < nlExclusionSymbol.getLength(); i++) {
    			Node node = nlExclusionSymbol.item(i);
    			if (node.getNodeType() == Node.ELEMENT_NODE) {
    				Element e = (Element) node;
    				NodeList nlName = e.getElementsByTagName("name");
    				NodeList nlAddress = e.getElementsByTagName("AddressType");
    				NodeList nlComment = e.getElementsByTagName("comment");
    				sName = nlName.item(0).getChildNodes().item(0).getNodeValue().trim();

    				if (nlName.getLength() > 0 && nlAddress.getLength() > 0 && nlComment.getLength() > 0)	{
    					sAddressType = nlAddress.item(0).getChildNodes().item(0).getNodeValue().trim();
    					sComment = nlComment.item(0).getChildNodes().item(0).getNodeValue().trim();
    					if (sSymName.toLowerCase().contains(sName.toLowerCase()) && PlcSymbols.getAddressType(sSymIONum).equals(sAddressType) && sSymComment.contains(sComment) )	{
    						return true;
    					}
    				}
    				else {

    					if (nlName.getLength() > 0 && nlAddress.getLength() > 0)	{
    						sAddressType = nlAddress.item(0).getChildNodes().item(0).getNodeValue().trim();
    						if (sSymName.toLowerCase().contains(sName.toLowerCase()) && PlcSymbols.getAddressType(sSymIONum).equals(sAddressType) )	{
    							return true;
    						}
    					}

    					else	{
    						if (sSymName.toLowerCase().contains(sName.toLowerCase()))	{
    							//fireOptionEvent(new OptionEvent(this,sSymName));
    							return true;
    						}
    					}
    				}
    			}
    		}
    	}
		return false;
    }
	
    
    ///check if plc symbol is excluded from "NotUsedInPLC" check 
    static public boolean isExNotUsedInPlc(Element eXmlExcl,  List<String> lstSymbols) throws SAXException {

		String sSymName = lstSymbols.get(0);
		String sSymIONum = lstSymbols.get(1);
		//String sSymType = lstSymbols.get(2);
		String sSymComment = lstSymbols.get(3);
    	
    	///check for symbol exclusions
    	NodeList nlExclusions = eXmlExcl.getElementsByTagName("NotUsedInPlc");
    	if (nlExclusions != null && nlExclusions.getLength() > 0 && lstSymbols.get(0) != "")	{

    		String sName, sAddressType, sComment;

    		for (int i = 0; i < nlExclusions.getLength(); i++) {
    			Node node = nlExclusions.item(i);
    			if (node.getNodeType() == Node.ELEMENT_NODE) {
    				Element e = (Element) node;
    				NodeList nlName = e.getElementsByTagName("ExName");
    				NodeList nlAddress = e.getElementsByTagName("ExAddressType");
    				NodeList nlComment = e.getElementsByTagName("ExComment");
					    				
    				if (nlName.getLength() > 0 && nlAddress.getLength() > 0 && nlComment.getLength() > 0)	{
    					sName = nlName.item(0).getChildNodes().item(0).getNodeValue().trim();
    					sComment = nlComment.item(0).getChildNodes().item(0).getNodeValue().trim();	
    					sAddressType = nlAddress.item(0).getChildNodes().item(0).getNodeValue().trim();
    					if (sSymName.toLowerCase().contains(sName.toLowerCase()) && PlcSymbols.getAddressType(sSymIONum).equals(sAddressType) && sSymComment.contains(sComment) )	{
    						return true;
    					}
    				}

    				else {

    					if (nlName.getLength() > 0 && nlAddress.getLength() > 0)	{
    						sName = nlName.item(0).getChildNodes().item(0).getNodeValue().trim();
    						sAddressType = nlAddress.item(0).getChildNodes().item(0).getNodeValue().trim();
    						if (sSymName.toLowerCase().contains(sName.toLowerCase()) && PlcSymbols.getAddressType(sSymIONum).equals(sAddressType) )	{
    							return true;
    						}
    					}

    					else	{
    						if (nlName.getLength() > 0)	{
    							sName = nlName.item(0).getChildNodes().item(0).getNodeValue().trim();
    							if (sSymName.toLowerCase().contains(sName.toLowerCase()))	{
    								//fireOptionEvent(new OptionEvent(this,sSymName));
    								return true;
    							}
    						}
    						else	{
    							if (nlComment.getLength() > 0) {
    								sComment = nlComment.item(0).getChildNodes().item(0).getNodeValue().trim();	    
    								if (sSymComment.toLowerCase().contains(sComment.toLowerCase()))	{
    									//fireOptionEvent(new OptionEvent(this,sSymName));
    									return true;
    								}
    							}
    						}

    					}
    				}

    			}
    		}   
    	}
        	///check for range exclusions
    	NodeList nlExclusionRange = eXmlExcl.getElementsByTagName("ExRange");

    	if (nlExclusionRange != null && nlExclusionRange.getLength() > 0 && lstSymbols.get(0) != "") {

    		String sAddressType1;
    		Float fStart, fEnd;

    		for (int i = 0; i < nlExclusionRange.getLength(); i++) {
    			Node node = nlExclusionRange.item(i);
    			if (node.getNodeType() == Node.ELEMENT_NODE) {
    				Element e = (Element) node;
    				NodeList nlStart = e.getElementsByTagName("ExStart");
    				NodeList nlEnd = e.getElementsByTagName("ExEnd");
    				NodeList nlAddress = e.getElementsByTagName("ExRngAddressType");

    				if (nlStart.getLength() > 0 && nlEnd.getLength() > 0 && nlAddress.getLength() > 0)	{

    					sAddressType1 = nlAddress.item(0).getChildNodes().item(0).getNodeValue().trim();
    					fStart = Float.parseFloat(nlStart.item(0).getChildNodes().item(0).getNodeValue().trim());
    					fEnd = Float.parseFloat(nlEnd.item(0).getChildNodes().item(0).getNodeValue().trim());

    					if (PlcSymbols.getAddress(sSymIONum) >= fStart && PlcSymbols.getAddress(sSymIONum) <= fEnd && PlcSymbols.getAddressType(sSymIONum).equals(sAddressType1) )	{
    						return true;
    					}
    				}
    			}
    		}
    	}
    	return false;
    }

}